﻿using System;
using System.Collections.Generic;
using System.Web;

namespace TCMSystem
{
    /// <summary>
    /// 模块的权限列表
    /// </summary>
    public enum ModulePowerType
    {
        None,

        CanRead,

        CoreMenu_New,
        CoreMenu_Edit,
        CoreMenu_Delete,

        CoreJobTitle_New,
        CoreJobTitle_Edit,
        CoreJobTitle_Delete,
        
        CoreDept_New,
        CoreDept_Edit,
        CoreDept_Delete,

        CoreRole_New,
        CoreRole_Edit,
        CoreRole_Delete,

        CoreUser_New,
        CoreUser_Edit,
        CoreUser_Delete,
        CoreUser_ChangePassword,

        CoreRoleUser_New,
        CoreRoleUser_Delete,

        CoreRoleModule_Edit,

        CoreRoleMenu_Edit,

        CoreLog_Delete,

        CoreConf_Edit,

        CorePassword_Edit,

        BizCompany_New,
        BizCompany_Edit,
        BizCompany_Delete,
        BizCompany_EditPicture,
        BizCompany_DeletePicture,
        BizCompany_UploadPicture,

        BizCommercial_New,
        BizCommercial_Edit,
        BizCommercial_Delete,

        BizEstate_New,
        BizEstate_Edit,
        BizEstate_Delete
    }

    public class ModulePowerTypeHelper
    {

        /// <summary>
        /// 其他权限的标题
        /// </summary>
        /// <param name="powerName"></param>
        /// <returns></returns>
        public static string GetTitle(string moduleName, string powerName)
        {
            return GetTitle(String2Power(String.Format("{0}_{1}", moduleName, powerName)));
        }

        /// <summary>
        /// 其他权限的标题
        /// </summary>
        /// <param name="power"></param>
        /// <returns></returns>
        public static string GetTitle(ModulePowerType power)
        {
            string result = String.Empty;

            string powerTypeName = ModulePowerTypeHelper.GetPowerTypeName(power);
            if (powerTypeName == "New")
            {
                result = "新增";
            }
            else if (powerTypeName == "Delete")
            {
                result = "删除";
            }
            else if (powerTypeName == "Edit")
            {
                result = "编辑";
            }
            else
            {
                switch (power)
                {
                    case ModulePowerType.CoreUser_ChangePassword:
                        result = "修改密码";
                        break;

                    case ModulePowerType.BizCompany_EditPicture:
                        result = "编辑厂区图片";
                        break;

                    case ModulePowerType.BizCompany_DeletePicture:
                        result = "删除厂区图片";
                        break;

                    case ModulePowerType.BizCompany_UploadPicture:
                        result = "上传厂区图片";
                        break;
                }
            }

            return result;
        }

        /// <summary>
        /// CoreUser_ChangePassword => ChangePassword
        /// </summary>
        /// <param name="modulePowerTypeName"></param>
        /// <returns></returns>
        public static string GetPowerTypeName(string modulePowerTypeName)
        {
            return GetPowerTypeName(modulePowerTypeName, String.Empty);
        }

        /// <summary>
        /// CoreUser_ChangePassword => ChangePassword
        /// </summary>
        /// <param name="powerType"></param>
        /// <returns></returns>
        public static string GetPowerTypeName(ModulePowerType powerType)
        {
            return GetPowerTypeName(ModulePowerTypeHelper.Power2String(powerType), String.Empty);
        }

        /// <summary>
        /// CoreUser_ChangePassword => ChangePassword
        /// CoreUser_ChangePassword, CoreRole => String.Empty
        /// </summary>
        /// <param name="powerType"></param>
        /// <param name="moduleTypeName"></param>
        /// <returns></returns>
        public static string GetPowerTypeName(ModulePowerType powerType, string moduleTypeName)
        {
            return GetPowerTypeName(ModulePowerTypeHelper.Power2String(powerType), moduleTypeName);
        }

        /// <summary>
        /// CoreUser_ChangePassword => ChangePassword
        /// CoreUser_ChangePassword, CoreRole => String.Empty
        /// </summary>
        /// <param name="modulePowerTypeName"></param>
        /// <param name="moduleTypeName"></param>
        /// <returns></returns>
        public static string GetPowerTypeName(string modulePowerTypeName, string moduleTypeName)
        {
            string powerTypeName = String.Empty;
            int underscoreIndex = modulePowerTypeName.IndexOf('_');
            if (underscoreIndex >= 0)
            {
                string moduleName = modulePowerTypeName.Substring(0, underscoreIndex);
                powerTypeName = modulePowerTypeName.Substring(underscoreIndex + 1);

                // 如果指定的模块前缀不相同
                if (!String.IsNullOrEmpty(moduleTypeName) && moduleName != moduleTypeName)
                {
                    powerTypeName = String.Empty;
                }

            }
            return powerTypeName;
        }

        /// <summary>
        /// 字符串转换为ModulePowerType
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static ModulePowerType String2Power(string text)
        {
            return String2Power(text, false);
        }

        /// <summary>
        /// 字符串转换为ModulePowerType
        /// </summary>
        /// <param name="text"></param>
        /// <param name="ignoreCase"></param>
        /// <returns></returns>
        public static ModulePowerType String2Power(string text, bool ignoreCase)
        {
            if (Enum.IsDefined(typeof(ModulePowerType), text))
            {
                return (ModulePowerType)Enum.Parse(typeof(ModulePowerType), text, ignoreCase);
            }
            else
            {
                return ModulePowerType.None;
            }
        }

        /// <summary>
        /// ModulePowerType转换为字符串
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static String Power2String(ModulePowerType type)
        {
            return type.ToString();
        }

        /// <summary>
        /// 获取所有权限的名称列表
        /// </summary>
        /// <returns></returns>
        public static string[] GetModulePowerTypes()
        {
            return Enum.GetNames(typeof(ModulePowerType));
        }

        private static Dictionary<string, List<string>> _moduleOtherPowerTypeDic = new Dictionary<string, List<string>>();

        /// <summary>
        /// 获得每个模块对应的其他权限名称列表
        /// </summary>
        /// <returns></returns>
        public static List<string> GetModuleOtherPowerTypeDic(string moduleTypeName)
        {
            if (!_moduleOtherPowerTypeDic.ContainsKey(moduleTypeName))
            {
                _moduleOtherPowerTypeDic[moduleTypeName] = new List<string>();

                foreach (string otherPower in GetModulePowerTypes())
                {
                    int underscoreIndex = otherPower.IndexOf('_');
                    if (underscoreIndex >= 0)
                    {
                        string moduleName = otherPower.Substring(0, underscoreIndex);
                        if (moduleTypeName == moduleName)
                        {
                            string powerName = otherPower.Substring(underscoreIndex + 1);
                            _moduleOtherPowerTypeDic[moduleTypeName].Add(powerName);
                        }
                    }
                }

            }

            return _moduleOtherPowerTypeDic[moduleTypeName];
        }


    }

}